// index.js
// 获取应用实例
const app = getApp();
const query = wx.createSelectorQuery();
var row = 0;
Page({
  data: {
    answer: "FREAK",
    word: "",
    info: [
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
      [
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
        { char: "", color: "" },
      ],
    ],
    green:"#FFCCCC",
    yellow:"#FFFFCC",
    grey:"#CCCCCC" ,
    focus:"true",
    setGiveup:false,
    setStart:true,
    setInput:false
  },
  inputWord: function (e) {
    let pwd = e.detail.value;
    this.setData({
      word: pwd.replace(/[^a-zA-Z]/g, ""),
    });
    return pwd.replace(/[^a-zA-Z]/g, "");
  },
  checkWord: function (e) {
    var myWord = e.detail.value.toUpperCase();
    if (myWord.length < 5) {
      return;
    }
    this.checkWordFromServer(myWord.toLowerCase()).then((flag) => {
      console.log(`check ${myWord}: ${flag}`);
      if (!flag) {
        wx.showModal({
          title: "error",
          content: "该单词不存在",
          showCancel: false,
        });
        return;
      }
      console.log(myWord);
      var i = 0;
      var colorValue = "";
      var that = this;
      var count = 0;
      for (; i < 5; i++) {
        let char = "info[" + row + "][" + i + "].char";
        let color = "info[" + row + "][" + i + "].color";
        let ch = myWord.substr(i, 1);
        let answerCh = that.data.answer.substr(i, 1);
        if (ch == answerCh) {
          colorValue = that.data.green;
          count++;
        } else if (that.data.answer.match(ch) != null) {
          colorValue = that.data.yellow;
        } else {
          colorValue = that.data.grey;
        }
        this.setData({
          [char]: myWord.substr(i, 1),
          [color]: colorValue,
        });
        that.setData({
          word: "",
        });
      }
      row++;
      if (count == 5) {
        wx.showModal({
          title: "答对了(●'◡'●)",
          content: "尝试再来一局吧！",
          showCancel: false,
        });
        this.setData({
          setStart: false,
          setGiveup: true,
          setInput: true,
        });
        return;
      }
      if (row == 6) {
        this.giveUp();
      }
    });
  },
  start: function () {
    var that = this;
    for (let i = 0; i < row; i++) {
      for (let j = 0; j < 5; j++) {
        let char = "info[" + i + "][" + j + "].char";
        let color = "info[" + i + "][" + j + "].color";
        that.setData({
          [char]: "",
          [color]: "",
        });
      }
    }
    row = 0;
    this.setData({
      setStart: true,
      setGiveup: false,
      setInput: false,
    });
    this.getWordFromServer();
  },
  giveUp: function () {
    var answer = this.data.answer;
    wx.showModal({
      title: "你输啦_(:з)∠)_",
      content: "答案：" + answer,
      showCancel: false,
    });
    this.setData({
      setStart: false,
      setGiveup: true,
      setInput: true,
    });
  },
  onShow() {
    console.log("page showing");
    this.getWordFromServer();
  },
  getWordFromServer: function () {
    wx.request({
      url: "http://localhost:8086/wordle/getWord",
      header: {
        "content-type": "application/json",
      },
      success: (res) => {
        console.log(res.data);
        let resWord = res.data.result.word;
        resWord = resWord.toUpperCase();
        console.log(`get: ${resWord}`);
        this.setData({ answer: resWord });
      },
      fail: () => {
        console.log("getWord: http request fail");
      },
    });
  },
  checkWordFromServer: function (word) {
    let reqURL = "http://localhost:8086/wordle/checkExist?word=" + word;
    return new Promise((resolve) => {
      wx.request({
        url: reqURL,
        header: {
          "content-type": "application/json",
        },
        success: (res) => {
          resolve(res.data.result.exist);
        },
        fail: () => {
          console.log("checkWord: http request fail");
        },
      });
    });
  },
});
